package com.qiantu.phone.ui.activity;

import android.content.Intent;
import android.view.View;
import android.widget.CheckBox;

import com.hjq.http.listener.HttpCallback;
import com.qiantu.api.entity.SettingBean;
import com.qiantu.api.http.LLHttpManager;
import com.qiantu.api.http.model.HttpData;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppActivity;

import okhttp3.Call;

/**
 * time   : 2021/12/22
 */
public final class SmartModeActivity extends AppActivity {

    private CheckBox btnNormalMode;
    private CheckBox btnLocalMode;
    private SettingBean.SettingDTO.IntelligentControlModeDTO data;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_smart_mode;
    }

    @Override
    protected void initView() {
        btnNormalMode = findViewById(R.id.btn_normal_mode);
        btnLocalMode = findViewById(R.id.btn_local_mode);
        setOnClickListener(btnNormalMode, btnLocalMode);
        int controlMode = getIntent().getIntExtra("controlMode", 0);
        updateView(controlMode);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_normal_mode:
                //点击后，选中状态保持原样，等接口返回成功后再切换
                btnNormalMode.setChecked(!btnNormalMode.isChecked());
                data.setControlMode(1);
                setting();
                break;
            case R.id.btn_local_mode:
                //点击后，选中状态保持原样，等接口返回成功后再切换
                btnLocalMode.setChecked(!btnLocalMode.isChecked());
                data.setControlMode(2);
                setting();
                break;
        }
    }

    @Override
    protected void initData() {
        LLHttpManager.getIntelligentControlModeSetting(this, new HttpCallback<HttpData<SettingBean.SettingDTO.IntelligentControlModeDTO>>(this) {
            @Override
            public void onStart(Call call) {
                showDialog(getString(R.string.common_loading));
            }

            @Override
            public void onSucceed(HttpData<SettingBean.SettingDTO.IntelligentControlModeDTO> result) {
                data = result.getData();
                updateView(data.getControlMode());
            }

            @Override
            public void onFail(Exception e) {
                super.onFail(e);
            }

            @Override
            public void onEnd(Call call) {
                hideDialog();
            }
        });
    }

    private void setting() {
        LLHttpManager.setIntelligentControlModeSetting(this, data, new HttpCallback<HttpData<Void>>(this) {
            @Override
            public void onStart(Call call) {
                showDialog(getString(R.string.setting));
            }

            @Override
            public void onSucceed(HttpData<Void> result) {
                toast(R.string.set_success);
                updateView(data.getControlMode());
                Intent intent = new Intent();
                intent.putExtra("controlMode", data.getControlMode());
                setResult(RESULT_OK, intent);
            }

            @Override
            public void onFail(Exception e) {
                super.onFail(e);
            }

            @Override
            public void onEnd(Call call) {
                hideDialog();
            }
        });
    }

    private void updateView(int controlMode) {
        if (data == null) {
            return;
        }
        //智控模式 1:普通模式 2：局域网模式
        switch (controlMode) {
            case 1:
                btnNormalMode.setChecked(true);
                btnLocalMode.setChecked(false);
                break;
            case 2:
                btnNormalMode.setChecked(false);
                btnLocalMode.setChecked(true);
                break;
        }
    }
}